package com.stbu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stbu.bean.UserBean;
import com.stbu.service.UserService;
import com.stbu.utils.RestResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RequiredArgsConstructor
@RestController
@CrossOrigin(origins = "*")
public class UserController {

    private final  UserService userService ;

    @RequestMapping("/getAdminById")
    public RestResponse getAdminById(){
        UserBean userBean = userService.getById(1);
        return RestResponse.ok(userBean);
    }

    @RequestMapping("/getAdminByName")
    public RestResponse getAdminByName(String uname,String upassword){
        if (uname==null || upassword==null) {
            return  RestResponse.fail("用户或密码为空");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uname",uname);
        queryWrapper.eq("upassword",upassword);
        UserBean userBean = userService.getOne(queryWrapper);
        if (userBean ==null) {
            return RestResponse.ok().setMsg("没有此用户");
        } else {
            return RestResponse.ok(userBean);
        }
    }

    @RequestMapping("/addUser")
    public RestResponse addUser(UserBean userBean) {
        int result = userService.addUser(userBean);
        if (result ==-1) {
            return RestResponse.fail("添加失败");
        } else {
            return  RestResponse.ok(result).setMsg("添加成功");
        }
    }
}
